import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { addNotice, delNotice, getNotice, listNotice, updateNotice } from '@/api/system/notice.js'

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'noticeId'
  const moduleName =  '公告'
  const apiFn = {
    listApiFn: listNotice,
    delApiFn: delNotice,
    apiFnConfig:{
      info: getNotice,
      add: addNotice,
      edit: updateNotice
    }
  }

  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
  }

}


/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_notice_type', 'sys_notice_status'
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: '公告标题',
      prop: 'noticeTitle',
      tableColumnProps: {
        align: 'center'
      },
      propInForm:{
        colProps:{
          span:12
        }
      }
    },
    {
      label: '公告类型',
      prop: 'noticeType',
      tableColumnProps: {
        align: 'center'
      },
      valueType: 'select',
      options: dictRawData.sys_notice_type,
      propInForm:{
        colProps:{
          span:12
        }
      }
    },

    {
      label: '状态1',
      prop: 'status',
      tableColumnProps: {
        align: 'center'
      },
      valueType: 'select',
      options: dictRawData.sys_notice_status,
      propInForm:{
        valueType:'radio',
        colProps:{
          span:12
        }
      }
    },
    {
      label: '公告内容',
      prop: 'noticeContent',
      tableColumnProps: {
        align: 'center'
      },
      hideInSearch: true,
      hideInTable:true,
      propInForm:{
        colProps:{
          span:24
        }
      }
    },
    {
      label: '创建时间',
      prop: 'createTime',
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: 'center'
      },
      valueType: 'date-picker',
      fieldProps: {
        type: 'daterange',
        valueFormat: 'YYYY-MM-DD'
      }
    },
    {
      label: '操作',
      prop: 'action',
      width: '160px',
      hideInSearch: true,
      hideInForm: true,
      tableColumnProps: {
        align: 'center',
        fixed: 'right'
      }
    }
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

